

//  /api/goods/list
//  /api/users/login

const http = require('http');

const url = require('url');

const path = require('path');

const fs = require('fs');


const server = http.createServer((req,res) => {

    const {query,pathname} = url.parse(req.url,true);
    // console.log(req);
    const {method} = req;
    if(/^\/api/.test(pathname)){
        if(pathname === '/api/goods/list' && method === 'GET'){
            
            const info = {
                code:1,
                message:"获取商品列表成功",
                data:[
                    {id:1,name:'手机'},{id:2,name:'电脑'},{id:3,name:'电视'}
                ]
            }
            res.end(JSON.stringify(info));

        }
    }   


    if(/^\/static/.test(pathname)){
        // base index.js
        // ext .js
        const {base,ext} = path.parse(pathname);
        if(ext === '.js'){
            // localhost:8899/static/index.js
            fs.readFile('./client/'+base,'utf-8',(err,data) => {
                if(err){
                    res.end('');
                    return;
                }
                res.end(data);
            })
        }
    }
    // localhost:9900/views/home // 不需要写 homt.html 
    // localhost:9900/views/list 
    if(/^\/views/.test(pathname)){
        const { base } = path.parse(pathname);
        // console.log(base);
        fs.readFile(`./client/${base}.html`,'utf8',(err,data)=>{
            if(err){
                res.end(404);
                return;
            }else{
                res.end(data);
            }
    })
    }


    if(method === 'GET' && pathname === '/api/news/list'){
        // current 
        // pagesize 
        // date 
        const info = {
            code:1,
            message:"获取新闻列表成功",
        }
        // localhost:8899/api/news/list?current=1&pagesize=10&date='2023-10-10'
        // console.log(query); 
        if(!query.current){
            info.code = 0;
            info.message = "没有写current参数";
        }else if(!query.pagesize){
            info.code = 0;
            info.message = "没有写pagesize参数";
        }else if(!query.date){
            info.code = 0;
            info.message = "没有写date参数";
        }else{
            info.data = [
                {id:1,title:'新闻1'},{id:2,title:'新闻2'},{id:3,title:'新闻3'}
            ]
            info.params = query;


        }

        res.end(JSON.stringify(info));



    }


    // api/users/login

    if(method === 'POST' && pathname === '/api/users/login'){
        //get 请求直接把参数放在url上面
        // post 请求把参数放在请求体里面

        // 为了保证能够收到数据 
        // req.on('data',function(){})  开始接收数据

        // req.on('end',function(){})  数据接收完毕
        let strs = '';
        // // req.on('data',function(chunk){ //开始接收数据
        // //     // console.log(chunk);
        // //     str += chunk;
        // //     console.log('666'+str);
        // })

        req.on('data',chunk=>strs+=chunk);
        //这一块来接收你传的post 参数  
        //chunk是一个buffer对象 通过 ''+chunk 转换成字符串 
        req.on('end',function(){ 
            // console.log('数据接收完毕');
            // aplication/x-www-form-urlencoded
            // console.log(strs);
            // aplication/json
            let params = null;
            if(req.headers['content-type'] === 'application/x-www-form-urlencoded'){
                // ?username=123&password=123
                // console.log(strs+'888')
                params = url.parse('?'+strs,true).query;
                // console.log(params);    
            }

            if(req.headers['content-type'] === 'application/json'){
                // {"username":"123","password":"123"}
                console.log(strs);
                params = JSON.parse(strs);
                console.log(params);
            }

            const info = {
                code:1,
                message:"登录成功",
                params
    
            }
            if(!params.username){
                info.code = 0;
                info.message = "没有写username参数";

            }
            else if(!params.password){
                info.code = 0;
                info.message = "没有写password参数";

            }else{
                res.end(JSON.stringify(info));
            }

            
            
    
            
        })

        
    }


});



server.listen(8899,() => {
    console.log('http://localhost:8899');
})